﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SVC_BLL;
using SVC_DAL;
using System.Data;

namespace SVC_BLL
{
    public class VendasBLL
    {
        
        public int codigo { get; set; }
        public int cliente { get; set; }
        public DateTime data { get; set; }
        public int usuario { get; set; }
        public int forma_pagamento { get; set; }
        public int parcelas { get; set; }
        public Double valor { get; set; }
        public String observacao { get; set; }
        public Double desconto { get; set; }
        public Double percentual { get; set; }
        public Double valorFinal { get; set; }
        public String ven_status { get; set; }
        public String ven_tipo { get; set; }

        VendasDAL objDAL = new VendasDAL();

        public VendasBLL()
        { }

        public void limpar()
        {
            try
            {
                this.codigo = 0;
                this.data = DateTime.Now;
                this.cliente = 0;
                this.usuario = 0;
                this.forma_pagamento = 0;
                this.parcelas = 0;
                this.valor = 0;
                this.observacao = null;
                this.desconto = 0;
                this.percentual = 0;
                this.valorFinal = 0;
                this.ven_status = null;
                this.ven_tipo = null;

            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public void inserir()
        {
            try
            {
                objDAL = new VendasDAL();
                this.codigo = objDAL.inserir(this.cliente, this.data, this.usuario, this.forma_pagamento, this.parcelas, this.valor, this.observacao, this.percentual, this.desconto, this.valorFinal, "Ativo", this.ven_tipo);
                objDAL = null;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void alterar()
        {
            try
            {
                objDAL = new VendasDAL();
                objDAL.alterar(this.codigo, this.cliente, this.data, this.usuario, this.forma_pagamento, this.parcelas, this.valor, this.observacao, this.percentual, this.desconto, this.valorFinal, this.ven_status, this.ven_tipo);
                objDAL = null;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void cancelarVenda()
        {
            try
            {
                objDAL = new VendasDAL();
                objDAL.cancelarVenda(this.codigo, this.ven_status);
                objDAL = null;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void alterarTipoVenda()
        {
            try
            {
                objDAL = new VendasDAL();
                objDAL.alterarTipoVenda(this.codigo,this.forma_pagamento,this.parcelas, this.ven_tipo);
                objDAL = null;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void excluir()
        {
            try
            {
                objDAL = new VendasDAL();
                objDAL.excluir(this.codigo);
                objDAL = null;
            }
            catch (Exception)
            {

                throw;
            }
        }


        public void localizar()
        {
            try
            {
                objDAL = new VendasDAL();
                this.codigo = objDAL.localizar(this.codigo);
                objDAL = null;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void localizar(String descricao, String atributo)
        {

            try
            {
                DataTable tab;
                objDAL = new VendasDAL();
                tab = objDAL.localizar(descricao, atributo);
                if (tab.Rows.Count > 0)
                {
                    this.codigo = int.Parse(tab.Rows[0]["ven_codigo"].ToString());
                    this.cliente = int.Parse(tab.Rows[0]["ven_cliente"].ToString());
                    this.data = DateTime.Parse(tab.Rows[0]["ven_dataVenda"].ToString());
                    this.usuario = int.Parse(tab.Rows[0]["ven_usuario"].ToString());
                    if (tab.Rows[0]["ven_formaPagamento"] != DBNull.Value)
                        this.forma_pagamento = int.Parse(tab.Rows[0]["ven_formaPagamento"].ToString());
                    else
                        this.forma_pagamento = 0;
                    if (tab.Rows[0]["ven_qtdParcelas"] != DBNull.Value)
                        this.parcelas = int.Parse(tab.Rows[0]["ven_qtdParcelas"].ToString());
                    else
                        this.parcelas = 0;
                    this.valor = Double.Parse(tab.Rows[0]["ven_valorTotal"].ToString());
                    this.observacao = tab.Rows[0]["ven_observacao"].ToString();
                    this.desconto = Double.Parse(tab.Rows[0]["ven_desconto"].ToString());
                    this.percentual = Double.Parse(tab.Rows[0]["ven_percentualDesconto"].ToString());
                    this.valorFinal= Double.Parse(tab.Rows[0]["ven_valorFinal"].ToString());
                    this.ven_status = tab.Rows[0]["ven_status"].ToString();
                    this.ven_tipo = tab.Rows[0]["ven_tipo"].ToString();
                }
                objDAL = null;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void localizarLeave(String descricao, String atributo)
        {

            try
            {
                DataTable tab;
                objDAL = new VendasDAL();
                tab = objDAL.localizarLeave(descricao, atributo);
                if (tab.Rows.Count > 0)
                {
                    this.codigo = int.Parse(tab.Rows[0]["ven_codigo"].ToString());
                    this.cliente = int.Parse(tab.Rows[0]["ven_cliente"].ToString());
                    this.data = DateTime.Parse(tab.Rows[0]["ven_dataVenda"].ToString());
                    this.usuario = int.Parse(tab.Rows[0]["ven_usuario"].ToString());
                    if (tab.Rows[0]["ven_formaPagamento"] != DBNull.Value)
                        this.forma_pagamento = int.Parse(tab.Rows[0]["ven_formaPagamento"].ToString());
                    else
                        this.forma_pagamento = 0;
                    if (tab.Rows[0]["ven_qtdParcelas"] != DBNull.Value)
                        this.parcelas = int.Parse(tab.Rows[0]["ven_qtdParcelas"].ToString());
                    else
                        this.parcelas = 0;
                    this.valor = Double.Parse(tab.Rows[0]["ven_valorTotal"].ToString());
                    this.observacao = tab.Rows[0]["ven_observacao"].ToString();
                    this.desconto = Double.Parse(tab.Rows[0]["ven_desconto"].ToString());
                    this.percentual = Double.Parse(tab.Rows[0]["ven_percentualDesconto"].ToString());
                    this.valorFinal = Double.Parse(tab.Rows[0]["ven_valorFinal"].ToString());
                    this.ven_status = tab.Rows[0]["ven_status"].ToString();
                    this.ven_tipo = tab.Rows[0]["ven_tipo"].ToString();
                }
                objDAL = null;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public DataTable localizarComRetorno(String descricao, String atributo)
        {
            try
            {
                DataTable tab;
                objDAL = new VendasDAL();
                tab = objDAL.localizar(descricao, atributo);
                objDAL = null;
                return tab;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public DataTable verificaLimiteClienteMensal(int cli_codigo)
        {
            try
            {
                DataTable tab;
                objDAL = new VendasDAL();
                tab = objDAL.verificaLimiteClienteMensal(cli_codigo);
                objDAL = null;
                return tab;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public DataTable verificaLimiteClienteTotal(int cli_codigo)
        {
            try
            {
                DataTable tab;
                objDAL = new VendasDAL();
                tab = objDAL.verificaLimiteClienteTotal(cli_codigo);
                objDAL = null;
                return tab;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public DataTable localizarEmTudo(String descricao)
        {
            try
            {
                DataTable tab;
                objDAL = new VendasDAL();
                tab = objDAL.localizarEmTudo(descricao);
                objDAL = null;
                return tab;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public DataTable FormarCupom(int venda)
        {
            try
            {
                DataTable tab;
                objDAL = new VendasDAL();
                tab = objDAL.FormarCupom(venda);
                objDAL = null;
                return tab;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void localizarPrimeiroUltimo(String descricao)
        {
            try
            {
                DataTable tab = null;
                int codigo = 0;
                objDAL = new VendasDAL();
                tab = objDAL.localizarPrimeiroUltimo(descricao);
                if (tab.Rows.Count > 0)
                    int.TryParse(tab.Rows[0][0].ToString(), out codigo);
                this.codigo = codigo;
                objDAL = null;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void localizarProxAnterior(String descricao, int codigo)
        {
            try
            {
                DataTable tab = null;
                objDAL = new VendasDAL();
                tab = objDAL.localizarProxAnterior(descricao, codigo);
                if (tab.Rows.Count > 0)
                    this.codigo = int.Parse(tab.Rows[0][0].ToString());
                objDAL = null;
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
